package com.solution.action;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.solution.constant.LoginConstants;

public class LoginErrorAction extends Action {

	Logger logger = Logger.getLogger(LoginFrontAction.class.getName());

	public ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		String forwardpage;
		try{
			HttpSession session = request.getSession();
			if(request.getParameter("error")!=null && "Deactive".equalsIgnoreCase(request.getParameter("error"))){
				request.setAttribute("invalidMsg","User is de-active.User is not allowed to log in.");
				forwardpage="success";
			}else if(request.getParameter("error")!=null && "Blocked".equalsIgnoreCase(request.getParameter("error"))){
				request.setAttribute("invalidMsg","User is blocked");
				forwardpage="success";
			}else{
				forwardpage="logout";
			}
			session.invalidate();
		}catch(Exception ex){
			logger.error("Error while updating user session(Exception)",ex);
			return mapping.findForward(LoginConstants.FORWARD_ACTION_ERROR);
		}
		return mapping.findForward(forwardpage);
	}
}
